package com.controller.frontdesk;

import com.bean.frontdesk.bo.LoginInBO;
import com.bean.frontdesk.bo.SignUpBO;
import com.bean.frontdesk.bo.UpdatePwdBO;
import com.bean.frontdesk.bo.UpdateUserBO;
import com.bean.frontdesk.vo.LoginVO;
import com.bean.frontdesk.vo.OrdersVO;
import com.bean.frontdesk.vo.SignUpVO;
import com.bean.frontdesk.vo.UpdateUserVO;
import com.bean.pojo.GoodsType;
import com.bean.pojo.User;
import com.bean.vo.admin.Result;
import com.google.gson.Gson;
import com.service.frontdesk.UserServiceImplement;
import com.utils.GsonUtils;
import com.utils.StringUtils;
import org.apache.commons.io.IOUtils;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;

@WebServlet("/api/mall/user/*")
public class UserServlet extends HttpServlet {
    private Gson gson = GsonUtils.getGson();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        String action = requestURI.replace(request.getContextPath() + "/api/mall/user/", "");
        if ("data".equals(action)){
            UpdateUser(request,response);
        }

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        String action = requestURI.replace(request.getContextPath() + "/api/mall/user/", "");
        if ("login".equals(action)){
            userLogin(request,response);
        }
        if ("signup".equals(action)){
            signUp(request,response);
        }
        if ("updatePwd".equals(action)){
            updatePwd(request,response);
        }
        if ("updateUserData".equals(action)){
            updateUser(request,response);
        }
        if ("logoutClient".equals(action)){
            logoutClient(request,response);
        }
    }

    /**
     * 退出登录
     * @param request
     * @param response
     */
    private void logoutClient(HttpServletRequest request, HttpServletResponse response) throws IOException {
        HttpSession session = request.getSession();
        session.removeAttribute("user");
        response.getWriter().println(gson.toJson(new Result(0,null,null)));

    }


    /**
     * 修改用户信息
     * @param request
     * @param response
     */
    private void updateUser(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ServletInputStream inputStream = request.getInputStream();
        String requestString = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
        UpdateUserBO updateUserBO = gson.fromJson(requestString, UpdateUserBO.class);
        UserServiceImplement userServiceImplement = new UserServiceImplement();
        int updateUserState = userServiceImplement.updateUser(updateUserBO);
        if (updateUserState==303){
            response.getWriter().println(gson.toJson(new Result(10000,null,"用户名已存在")));
        return;
        }
        if (updateUserState==200){
            response.getWriter().println(gson.toJson(new Result(0,null,null)));
            return;
        }
        if (updateUserState==101){
            response.getWriter().println(gson.toJson(new Result(10000,null,"用户名不能为空")));
        }
        response.getWriter().println(gson.toJson(new Result(10000,null,"修改失败")));


    }

    /**
     * 修改密码
     * @param request
     * @param response
     */
    private void updatePwd(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ServletInputStream inputStream = request.getInputStream();
        String requestString = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
        UpdatePwdBO updatePwdBO = gson.fromJson(requestString, UpdatePwdBO.class);
        UserServiceImplement userServiceImplement = new UserServiceImplement();
        int updatePwdState = userServiceImplement.updatePwd(updatePwdBO);
        if (updatePwdState==200){
            response.getWriter().println(gson.toJson(new Result(0,null,null)));
            return;
        }
        if (updatePwdState==330){
            response.getWriter().println(gson.toJson(new Result(10000,null,"旧密码不正确")));
            return;
        }
        response.getWriter().println(gson.toJson(new Result(10000,null,"修改失败")));


    }

    /**
     * 用户修改信息
     * @param request
     * @param response
     */
    private void UpdateUser(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String token = request.getParameter("token");
        UserServiceImplement userServiceImplement = new UserServiceImplement();
        UpdateUserVO updateUserVO = userServiceImplement.showUserUpdate(token);
        response.getWriter().println(gson.toJson(new Result(0,updateUserVO,null)));

    }



    /**
     * 注册
     * @param request
     * @param response
     */
    private void signUp(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ServletInputStream inputStream = request.getInputStream();
        String requestString = IOUtils.toString(inputStream,StandardCharsets.UTF_8);
        SignUpBO signUpBO = gson.fromJson(requestString, SignUpBO.class);
        UserServiceImplement userServiceImplement = new UserServiceImplement();
        int signState = userServiceImplement.signUp(signUpBO);
        if (signState==200){
            HttpSession session = request.getSession();
            session.setAttribute("user",signUpBO.getEmail());
            SignUpVO signUpVO = new SignUpVO(0, signUpBO.getNickname(), signUpBO.getNickname());
            response.getWriter().println(gson.toJson(new Result(0,signUpVO,null)));
            return;
        }
        response.getWriter().println(gson.toJson(new Result(10000,null,"注册失败")));

    }

    /**
     * 用户登录
     * @param request
     * @param response
     */
    private void userLogin(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ServletInputStream inputStream = request.getInputStream();
        String requestString = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
        LoginInBO loginInBO = gson.fromJson(requestString, LoginInBO.class);
        if (StringUtils.idEmpty(loginInBO.getEmail())||StringUtils.idEmpty(loginInBO.getPwd())){
            response.getWriter().println(gson.toJson(new Result(10000,null,"用户名或密码不能为空")));
            return;
        }
        UserServiceImplement userServiceImplement = new UserServiceImplement();
        User user = userServiceImplement.login(loginInBO);
       if (user!=null) {
           HttpSession session = request.getSession();
           session.setAttribute("user",user.getUsername());
           LoginVO loginVO = new LoginVO(0, user.getNickname(), user.getNickname());
           response.getWriter().println(gson.toJson(new Result(0, loginVO, null)));
           return;
       }

        response.getWriter().println(gson.toJson(new Result(10000,null,"用户名或者密码错误")));

    }

}
